home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / term-source.lha / termMemory.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  2KB  |  123 lines

  1. /*
  2. **    termMemory.c
  3. **
  4. **    Memory management routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     // Main memory pool puddle size
  13.  
  14. #define PUDDLE_SIZE 32768
  15.  
  16.     // Memory allocation data
  17.  
  18. STATIC struct SignalSemaphore    MemorySemaphore;
  19. STATIC APTR            MemoryPool;
  20.  
  21.     // Prototypes for pools.lib
  22.  
  23. APTR __asm AsmCreatePool(register __d0 ULONG MemFlags,register __d1 ULONG PuddleSize,register __d2 ULONG ThreshSize,register __a6 struct ExecBase *SysBase);
  24. VOID __asm AsmDeletePool(register __a0 APTR PoolHeader,register __a6 struct ExecBase *SysBase);
  25. APTR __asm AsmAllocPooled(register __a0 APTR PoolHeader, register __d0 ULONG Size,register __a6 struct ExecBase *SysBase);
  26. VOID __asm AsmFreePooled(register __a0 APTR PoolHeader,register __a1 APTR Memory,register __d0 ULONG MemSize,register __a6 struct ExecBase *SysBase);
  27.  
  28.     /* MemorySetup():
  29.      *
  30.      *    Set up the main memory pool.
  31.      */
  32.  
  33. BOOLEAN
  34. MemorySetup()
  35. {
  36.     InitSemaphore(&MemorySemaphore);
  37.  
  38.     if(MemoryPool = AsmCreatePool(MEMF_PUBLIC | MEMF_ANY,PUDDLE_SIZE,PUDDLE_SIZE,SysBase))
  39.         return(TRUE);
  40.     else
  41.         return(FALSE);
  42. }
  43.  
  44.     /* MemoryCleanup():
  45.      *
  46.      *    Clean up the main memory pool.
  47.      */
  48.  
  49. VOID
  50. MemoryCleanup()
  51. {
  52.     if(MemoryPool)
  53.     {
  54.         AsmDeletePool(MemoryPool,SysBase);
  55.  
  56.         MemoryPool = NULL;
  57.     }
  58. }
  59.  
  60.     /* FreeVecPooled(APTR Memory):
  61.      *
  62.      *    Free a memory chunk.
  63.      */
  64.  
  65. VOID __regargs
  66. FreeVecPooled(APTR Memory)
  67. {
  68.     if(Memory && MemoryPool)
  69.     {
  70.         ULONG *Data = (ULONG *)Memory;
  71.  
  72.         ObtainSemaphore(&MemorySemaphore);
  73.  
  74.         AsmFreePooled(MemoryPool,&Data[-1],Data[-1],SysBase);
  75.  
  76.         ReleaseSemaphore(&MemorySemaphore);
  77.     }
  78. }
  79.  
  80.     /* AllocVecPooled(ULONG Size,ULONG Flags):
  81.      *
  82.      *    Allocate a memory chunk from the main memory pool and
  83.      *    remember its size.
  84.      */
  85.  
  86. APTR __regargs
  87. AllocVecPooled(ULONG Size,ULONG Flags)
  88. {
  89.     if(MemoryPool)
  90.     {
  91.         ULONG *Data;
  92.  
  93.         Size = (Size + 3 + sizeof(ULONG)) & ~3;
  94.  
  95.         ObtainSemaphore(&MemorySemaphore);
  96.  
  97.         if(Data = (ULONG *)AsmAllocPooled(MemoryPool,Size,SysBase))
  98.         {
  99.             ReleaseSemaphore(&MemorySemaphore);
  100.  
  101.             *Data++ = Size;
  102.  
  103.                 // Zero the chunk if necessary
  104.  
  105.             if(Flags & MEMF_CLEAR)
  106.             {
  107.                 register ULONG *Memory = Data;
  108.  
  109.                 Size /= sizeof(ULONG);
  110.  
  111.                 while(--Size)
  112.                     *Memory++ = 0;
  113.             }
  114.  
  115.             return((APTR)Data);
  116.         }
  117.  
  118.         ReleaseSemaphore(&MemorySemaphore);
  119.     }
  120.  
  121.     return(NULL);
  122. }
  123.